
/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：用户视频认证                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-02-07 18:42:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Services                                  
*│　类    名： VideoService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using BestTrader.Cms.IRepository;
using BestTrader.Cms.IServices;
using BestTrader.Cms.Models;
using BestTrader.Cms.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;

namespace BestTrader.Cms.Services
{
    public class VideoService : IVideoService
    {
        private readonly IVideoRepository _repository;

        public VideoService(IVideoRepository repository)
        {
            _repository = repository;
        }

        public TableDataResult<Video> GetVideoUser()
        {
            var result = new TableDataResult<Video>();
            var now = DateTime.Now;
            var video = _repository.Get($"select top 1 * from Video where State = 0 order by AddTime desc");
            if (video != null)
            {
                if (now < video.AddTime.AddMinutes(5))
                {
                    result.Data = video;
                }
            }
            return result;
        }

        public BooleanResult ValidVideo(String userId)
        {
            bool data = false;
            var _userId = 0;
            var now = DateTime.Now;
            if (int.TryParse(userId, out _userId))
            {
                //var video = _repository.Get($"select top 1 * from Video where ManagerId = { userId } and State = 0 order by AddTime desc");
                var video = _repository.Get($"select top 1 * from Video where State = 0 order by AddTime desc");
                if (video == null)
                {
                    data = true;
                    _repository.Insert(new Models.Video {
                        ManagerId = _userId,
                        State = false,
                        AddTime = now,
                        AddManagerId = _userId,
                    });
                }
                else if (now > video.AddTime.AddMinutes(5))
                {
                    data = true;
                    video.State = true;
                    video.ModifyManagerId = _userId;
                    video.ModifyTime = now;
                    _repository.Update(video);
                }
            }
            var result = new BooleanResult
            {
                Data = data,
            };
            return result;
        }
    }
}